<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class msetup extends CI_Model{
    function __construct() {
        parent::__construct();
    }
    function getcombo($type="", $param1="", $param2="", $param3=""){
		$where = "WHERE 1=1 ";
		$join  = "";
		switch($type){
			case 'idx_user_group':
				$id = 'A.id';
				$value = 'A.group';			
			break;
                        case 'idx_user_subgroup':
				$id = 'A.id';
				$value = 'A.sub_group';			
			break;
			case 'idx_user_level':
				$id = 'A.id';
				$value = 'A.level';			
			break;
                        case 'idx_user_role':
				$id = 'A.id';
				$value = 'A.role';			
			break;
                        case 'idx_user_member':
				$id = 'A.id';
				$value = 'A.tbl_role_id';			
			break;
                        case 'tbl_user_modules':
				$id = 'A.id';
				$value = 'A.module_name';			
			break;
			
		}
		
		$sql = "
			SELECT ".$id." as code, 
					".$value." as value
			FROM ".$type." A
			$join
			$where
		";
                //echo $sql; exit();
		return $this->db->query($sql)->result_array();
	}
        //Fungsi Database
        function listData($type){
            
        }
        function saveData($type,$data,$param=""){
            $editstatus=$data["editstatus"];
            $table="";
            switch($type){
                case "user_list":
                    //$editstatus=$data['id'];
                    $table="tbl_user"; 
                    break; 
                case "user_member":
                   
                    for($i=0;$i<count($data["member"]);$i++){
                        
                        $dataInput=array();
                        $dataInput = array("tbl_role_id"=>$data["member"][$i],
                                            "username"=>$data["user"]);
                        if($data['id'] == 'add') {
                            $this->db->insert("idx_user_member",array("tbl_role_id"=>$data["member"][$i],
                                            "username"=>$data["user"]));
                        }else{
                            $this->db->update($table, $data, array('id'=>$data['id']));
                        }
                                                
                    }
                    break;
                case "user_level":
                    //$editstatus=$data['id'];
                    $table="idx_user_level"; 
                    break;
                case "user_group":
                    //$editstatus=$data['ed'];
                    unset($data['ed']);
                    $table="idx_user_group";
                    break;
                case "user_role":
                    //$editstatus=$data['id'];
                    $table="idx_user_role";
                    break;
                case "app_modules":
                    //$editstatus=$data['id'];
                    $table="tbl_user_modules";                    
                    break;
                case "app_functions":
                    //$editstatus=$data["id"];
                    $table="tbl_user_module_functions";
                    break;
            }
            if($type !="user_member"){
                $this->db->trans_begin();
                if($editstatus == 'add') {
                    unset($data["editstatus"]);
                    if($type !="user_group"){
                        unset($data["id"]);
                    }
                    $this->db->insert($table, $data);
                }elseif($editstatus == 'edit'){
                    unset($data["editstatus"]);
                    $this->db->update($table, $data, array('id'=>$data['id']));
                }
                if($this->db->trans_status() == false){
                    $this->db->trans_rollback();
                    return "Data not saved";

                }else{
                    return $this->db->trans_commit();
                }
                
            }
        }
        function getdata($param="",$id=""){
            $where="";
            switch($param){
                
                case "user_list":
                    if($id!=""){
                        $where=" WHERE USR.id='$id'";
                    }
                    $sql = "SELECT USR.*,L.level,G.group,SG.sub_group FROM tbl_user USR"
                        . " LEFT JOIN idx_user_level L ON L.id=USR.user_level_id"
                        . " LEFT JOIN idx_user_group G ON G.id=USR.group_user_id"
                        . " LEFT JOIN idx_user_subgroup SG ON SG.id=USR.sub_group_id"
                        . " $where";
                    break;
                case "user_level":
                    if($id!=""){
                        $where=" WHERE id='$id'";
                    }
                    $sql ="SELECT * FROM idx_user_level $where";
                    break;
                case "user_group":
                    if($id!=""){
                        $where=" WHERE id='$id'";
                    }
                    $sql="SELECT * FROM idx_user_group $where";
                    break;
                case "user_role":
                    if($id!=""){
                        $where=" WHERE id='$id'";
                    }
                    $sql="SELECT * FROM idx_user_role $where";
                    break;
                case "app_modules":
                    if($id!=""){
                        $where=" WHERE id='$id'";
                    }
                    $sql="SELECT * FROM tbl_user_modules $where";
                    break;
                case "app_functions":
                    if($id!=""){
                        $where=" WHERE F.id='$id'";
                    }
                    $sql="SELECT F.*,M.module_name FROM tbl_user_module_functions F"
                        . " LEFT JOIN tbl_user_modules M ON M.id=F.tbl_module_id $where";
                    break;
                
            }
            if($id ==""){
                return $this->mdealer->get_json($sql); 
            }else{
                return $this->db->query($sql)->row_array();
            }
	}
        function deleteData($type,$param){
            
        }
}

